package com.yan_jiu_sheng.JavaAdvance.b_synchronized;

/**
 * @author yulongTian
 * @create 2024-05-26 13:19
 */
public class MyTicket implements Runnable {

    private int tickets = 100;

    Object obj = new Object();


    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (obj){
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "买了第" + tickets + "张票");
                    tickets--;
                }
            }
        }
    }
}
